package com.ccq.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ccq.domain.FieldBase;
import com.ccq.repository.FieldMapper;

@RestController
public class FieldController {
	@Autowired
	private FieldMapper fieldMapper;

	@GetMapping("/getField1")
	/* 这种返回是map机构 Map<key,List<FieldBase>> */
	public Map<String, List<FieldBase>> getField1() {

		List<FieldBase> list = fieldMapper.getFieldBase();
		Set<String> set = new HashSet<>();
		for (FieldBase fieldbase : list) {
			if (fieldbase.getGroupName() == null || fieldbase.getGroupName().equals("")) {
				set.add("其他");
			} else {
				set.add(fieldbase.getGroupName());
			}
		}
		Map<String, List<FieldBase>> map = new HashMap<>();

		Object[] array = set.toArray();
		for (int i = 0; i < array.length; i++) {
			List<FieldBase> li = new ArrayList<>();
			for (int j = 0; j < list.size(); j++) {
				if (array[i].equals("其他")) {
					if (list.get(j).getGroupName() == null || list.get(j).getGroupName().equals("")) {
						li.add(list.get(j));
					}
				} else if (array[i].equals(list.get(j).getGroupName())) {
					li.add(list.get(j));
				}
			}
			map.put(String.valueOf(array[i]), li);
		}
		return map;
	}

	@GetMapping("/getField2")
	public List<List<FieldBase>> getField2() {

		List<FieldBase> list = fieldMapper.getFieldBase();
		Set<String> set = new HashSet<>();
		for (FieldBase fieldbase : list) {
			if (fieldbase.getGroupName() == null || fieldbase.getGroupName().equals("")) {
				set.add("其他");
			} else {
				set.add(fieldbase.getGroupName());
			}
		}
		List<List<FieldBase>> Alist = new ArrayList<>();

		Object[] array = set.toArray();
		for (int i = 0; i < array.length; i++) {
			List<FieldBase> li = new ArrayList<>();
			for (int j = 0; j < list.size(); j++) {
				if (array[i].equals("其他")) {
					if (list.get(j).getGroupName() == null || list.get(j).getGroupName().equals("")) {
						li.add(list.get(j));
					}
				} else if (array[i].equals(list.get(j).getGroupName())) {
					li.add(list.get(j));
				}
			}
			/* 这和上面的方法没有任何区别，就这一个地方不同,只是把他放入一个list就可以了 */
			Alist.add(li);
		}
		return Alist;
	}
/**
 * 内部类方便好看
 *
 */
	public class Field {
		private String name;
		private List<FieldBase> list;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public List<FieldBase> getList() {
			return list;
		}
		public void setList(List<FieldBase> list) {
			this.list = list;
		}
		

	}

	@GetMapping("/getField3")
	public List<Field> getField3() {

		List<FieldBase> list = fieldMapper.getFieldBase();
		Set<String> set = new HashSet<>();
		for (FieldBase fieldbase : list) {
			if (fieldbase.getGroupName() == null || fieldbase.getGroupName().equals("")) {
				set.add("其他");
			} else {
				set.add(fieldbase.getGroupName());
			}
		}
		/**
		 * 这个Alist就放入你要返回的那个对象
		 */
		List<Field> Alist = new ArrayList<>();

		Object[] array = set.toArray();
		for (int i = 0; i < array.length; i++) {
			List<FieldBase> li = new ArrayList<>();
			Field field =new Field();
			for (int j = 0; j < list.size(); j++) {
				if (array[i].equals("其他")) {
					if (list.get(j).getGroupName() == null || list.get(j).getGroupName().equals("")) {
						li.add(list.get(j));
					}
				} else if (array[i].equals(list.get(j).getGroupName())) {
					li.add(list.get(j));
				}
			}
			/* 这和上面的方法也没什么区别，只不过给对象赋值，然后放入Alist 完事 */
			field.setName(array[i].toString());
			field.setList(li);
			Alist.add(field);
		}
		return Alist;
	}

}
